package com.supermap.dataservice.cloud.cust.menu;



import com.supermap.dubbo.api.cloud.cust.menu.SysMenu;
import com.supermap.dubbo.api.cloud.cust.menu.TreeNode;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @desc
 * @date 2019年02月21 10:31
 * @email 1301584033@qq.com
 */
@Mapper
public interface SysMenuMapper {


    @Select("select category from sys_menu where id=#{menuid} ")
    Integer iscategory(@Param("menuid")Integer menuid);


    @Select("select id from sys_menu where code like concat((select code from sys_menu where id=#{menuid}),\"%\") ")
    List<Integer> getCodes(@Param("menuid")Integer menuid);
    //查询改用户是否有、

    @Select("select menuid from sys_area_role s1\n" +
            "inner join sys_area_role_menu s2\n" +
            "on s1.id = s2.arearoleid\n" +
            "where s1.areacode = (select left(areaCode,2) from  t_user where id=#{userId})  and s1.roleid = (select roleid from t_user_role where userid=#{userId} ) and s2.menuid = #{menuid} ")
    Integer putIsExist(@Param("userId")Integer userId,@Param("menuid")Integer menuid);

    //查询菜单前端树
    @SelectProvider(type = SysMenuMapperSQL.class,method = "getSysMenuListSQL")
    List<SysMenu> getSysMenuList(@Param("userId")Integer userId,@Param("length") Integer length,@Param("code")String code);

    //添加功能定制
    @Insert("insert into sys_user_order_menu(menuid,userid) values(#{menuid},#{userid})")
    Integer putSysUserOrderMenu(@Param("menuid")Integer menuid,@Param("userid")Integer userId);
    //同步用户总菜单
    @Insert("insert into sys_user_menu(menuid,userid) values(#{menuid},#{userid})")
    Integer putSysMenu(@Param("menuid")Integer menuid,@Param("userid")Integer userId);

    //查询区域菜单
    @SelectProvider(type = SysMenuMapperSQL.class,method = "getAreaSysMenuListSQL")
    List<SysMenu> getAreaSysMenuList(@Param("areaCode")String areaCode,@Param("length") Integer length,@Param("code")String code,@Param("userId")Integer userId);
    //删除菜单
    @Delete("delete  from sys_user_menu where userid=#{userid} and menuid=#{menuid}")
    Integer delUserMenu(@Param("menuid")Integer menuid,@Param("userid")Integer userId);
    //删除定制目录
    @Delete("delete  from sys_user_order_menu where userid=#{userid} and menuid=#{menuid}")
    Integer delCostomMenu(@Param("menuid")Integer menuid,@Param("userid")Integer userId);

    //获取区域编码
    @Select("select code from sys_menu where id=#{menuid}")
    String getCodeByMenuId(@Param("menuid")Integer menuid);

    //
    @Select("select t1.areacode as id,t1.areaname as name, (select count(id) from sys_area_menu where areacode=t1.areacode and menuid= #{menuid}) as category from t_area t1  where length(t1.areacode)=2 ")
    List<TreeNode> getAllArea(@Param("menuid")Integer menuid);

    //查询是否有该目录
    @Select("select count(t1.id) from sys_menu t1 " +
            "inner join sys_user_menu t2 " +
            "on t1.id=t2.menuid " +
            "where t2.userid=#{userid} and t1.code =#{code} and t1.category=1 and t1.state=0")
    Integer getExitMenu(@Param("userid")Integer userId,@Param("code")String code);

    //查询是否有该目录
    @Select("select count(t1.id) from sys_menu t1 " +
            "inner join sys_area_menu t2 " +
            "on t1.id=t2.menuid " +
            "where t2.areacode=#{areaCode} and t1.code =#{code} and t1.category=1 and t1.state=0")
    Integer getExitAreaMenu(@Param("areaCode")String areaCode,@Param("code")String code);



    @Select("select id from sys_menu where code=#{code}")
    Integer getMenuIdByCode(@Param("code")String code);

    //删除菜单
    @Delete("delete from sys_area_menu where menuid=#{menuid}")
    Integer delSysAreaMenu(@Param("menuid")Integer menuid);
    //
    @Insert("insert into sys_area_menu(menuid,areacode) values(#{menuid},#{areaCode})")
    Integer putSysMenuArea(@Param("menuid")Integer menuid,@Param("areaCode")String areaCode);



    @SelectProvider(type = SysMenuMapperSQL.class,method = "getSysMenuTreeNodeListSQL")
    List<TreeNode> getSysMenuTreeNodeList(@Param("areaCode") String areaCode, @Param("roleId") Integer roleId, @Param("length") Integer length,@Param("code")String code);

    @Select("select code from sys_menu where length(code)=#{length} and code like concat(#{code},'%') order by code desc limit 1")
    String getCode(@Param("length")Integer length,@Param("code")String code);

    @Select("select * from sys_menu where id=#{menuId}")
    SysMenu  getSysMenuById(@Param("menuId")Integer menuId);

    @InsertProvider(type = SysMenuMapperSQL.class,method = "insertSysMenuSQL")
    Integer insertSysMenu(@Param("sysMenu") SysMenu sysMenu);

    @UpdateProvider(type = SysMenuMapperSQL.class,method = "updateSysMenuSQL")
    Integer updateSysMenu(@Param("sysMenu") SysMenu sysMenu);

    class SysMenuMapperSQL{
        /**
         * 查询功能定制树
         * */
        public String getAreaSysMenuListSQL(Map<String,Object> map){
            String code=(String)map.get("code");
            String areaCode=(String)map.get("areaCode");
            Integer userId=(Integer)map.get("userId");
            Integer length=(Integer)map.get("length");
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(" select t1.id,t1.name,t1.code,t1.url,t1.category,t1.functiontype,t1.icon,t1.orderNum,date_format(t1.createtime,'%Y-%m-%d %H:%i:%S') as createtime,t1.createuser,date_format(t1.updatetime,'%Y-%m-%d %H:%i:%S') as updatetime,t1.updateuser,(select count(id) from sys_user_menu where userid="+userId+" and menuid=t1.id) as state from sys_menu t1 " +
                    "  inner join sys_area_menu t2  " +
                    "   on t1.id=t2.menuid where  1=1 and t1.state=0 ");
            if(areaCode!=null){
                stringBuilder.append(" and t2.areacode like concat(#{areaCode},'%') ");
            }

            if(code!=null){
                stringBuilder.append(" and t1.code like concat('"+code+"','%') ");
            }
            if(length!=null){
                stringBuilder.append(" and length(t1.code)="+length+"  ");
            }
            return stringBuilder.toString();
        }
        /**
         * 查询前端APP菜单树
         * */

        public String getSysMenuListSQL(Map<String,Object> map){
            String code=(String)map.get("code");
            Integer userId=(Integer)map.get("userId");
            Integer length=(Integer)map.get("length");
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append("select t1.id,t1.name,t1.code,t1.url,t1.category,t1.functiontype,t1.icon,t1.orderNum,date_format(t1.createtime,'%Y-%m-%d %H:%i:%S') as createtime,t1.createuser,date_format(t1.updatetime,'%Y-%m-%d %H:%i:%S') as updatetime,t1.updateuser from sys_menu t1\n" +
                    "inner join sys_user_menu t2\n" +
                    "on t1.id=t2.menuid\n" +
                    "where 1=1 and t1.state=0 ");
            if(userId!=null){
                stringBuilder.append(" and t2.userid="+userId+" ");
            }

            if(code!=null){
                stringBuilder.append(" and t1.code like concat('"+code+"','%') ");
            }
            if(length!=null){
                stringBuilder.append(" and length(t1.code)="+length+"  ");
            }

            return stringBuilder.toString();
        }
        /**
         * 查询
         * */
        public String getSysMenuTreeNodeListSQL(Map<String,Object> map){
            String areaCode=(String)map.get("areaCode");
            String code=(String)map.get("code");
            Integer roleId=(Integer)map.get("roleId");
            Integer length=(Integer)map.get("length");

            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(" select t1.id,t1.name,'' as pId,t1.code,t1.category from sys_menu  t1 " +
                    "Left join sys_area_menu t2 " +
                    "on t1.id=t2.menuid " +
                    "Left join sys_area_role t3 " +
                    "on t2.areacode=t3.areacode " +
                    "where 1=1 and t1.state=0 ");
            if(areaCode!=null){
                stringBuilder.append("  and t2.areacode like concat('"+areaCode+"','%') ");
                if(roleId!=null){
                    stringBuilder.append("  and t3.roleid="+roleId+" ");
                }
            }

            if(code!=null){
                stringBuilder.append("  and t1.code like concat('"+code+"','%') ");
            }
            stringBuilder.append(" and  length(t1.code)="+length+" and t1.state=0  group by t1.id ");
            return stringBuilder.toString();
        }

        /**
         * 修改
         * */
        public String updateSysMenuSQL(Map<String,Object> map){
            SysMenu sysMenu=(SysMenu)map.get("sysMenu");
            StringBuilder stringBuilder= new StringBuilder();
            stringBuilder.append("update sys_menu set id="+sysMenu.getId()+"");
            if(sysMenu.getName()!=null){
                stringBuilder.append(" , name='"+sysMenu.getName()+"' ");
            }
            if(sysMenu.getCode()!=null){
                stringBuilder.append(" , code='"+sysMenu.getCode()+"' ");
            }
            if(sysMenu.getUrl()!=null){
                stringBuilder.append(" , url='"+sysMenu.getUrl()+"' ");
            }
            if(sysMenu.getCategory()!=null){
                stringBuilder.append(" , category="+sysMenu.getCategory()+" ");
            }
            if(sysMenu.getFunctiontype()!=null){
                stringBuilder.append(" , functiontype="+sysMenu.getFunctiontype()+" ");
            }
            if(sysMenu.getOrderNum()!=null){
                stringBuilder.append(" , orderNum="+sysMenu.getOrderNum()+" ");
            }
            if(sysMenu.getState()!=null){
                stringBuilder.append(" , state="+sysMenu.getState()+" ");
            }
            if(sysMenu.getUpdateuser()!=null){
                stringBuilder.append(" , updateuser="+sysMenu.getUpdateuser()+" ");
            }

            stringBuilder.append(" where id="+sysMenu.getId()+"");

            return  stringBuilder.toString();
        }

        /**
         * 添加
         * */
        public String insertSysMenuSQL(Map<String,Object> map){
            SysMenu sysMenu=(SysMenu)map.get("sysMenu");
            StringBuilder stringBuilder=new StringBuilder();
            StringBuilder title=new StringBuilder();
            StringBuilder val=new StringBuilder();
            stringBuilder.append(" insert into sys_menu ");
            if(sysMenu.getName()!=null){
                title.append("name,");
                val.append("'"+sysMenu.getName()+"',");
            }
            if(sysMenu.getCode()!=null){
                title.append("code,");
                val.append("'"+sysMenu.getCode()+"',");
            }
            if(sysMenu.getUrl()!=null){
                title.append("url,");
                val.append("'"+sysMenu.getUrl()+"',");
            }
            if(sysMenu.getCategory()!=null){
                title.append("category,");
                val.append(""+sysMenu.getCategory()+",");
            }
            if(sysMenu.getFunctiontype()!=null){
                title.append("functiontype,");
                val.append(""+sysMenu.getFunctiontype()+",");
            }
            if(sysMenu.getIcon()!=null){
                title.append("icon,");
                val.append("'"+sysMenu.getIcon()+"',");
            }
            if(sysMenu.getOrderNum()!=null){
                title.append("orderNum,");
                val.append(""+sysMenu.getOrderNum()+",");
            }
            if(sysMenu.getCreateuser()!=null){
                title.append("createuser,");
                val.append("'"+sysMenu.getCreateuser()+"',");
            }
            if(sysMenu.getUpdateuser()!=null){
                title.append("updateuser,");
                val.append("'"+sysMenu.getUpdateuser()+"',");
            }
            stringBuilder.append("("+title.substring(0,title.lastIndexOf(","))+") values("+val.substring(0,val.lastIndexOf(","))+")");
            return  stringBuilder.toString();
        }
    }
}
